package imgsei.wcj.com.commonbusiness.helper;

import android.content.Context;

import com.google.gson.GsonBuilder;

import imgsei.wcj.com.commonbusiness.base.http.ServiceApi;
import imgsei.wcj.com.commonbusiness.interfaces.service.Service;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitHelper {
    //当前上下文
    private Context context;

    private static RetrofitHelper instance = null;
    GsonConverterFactory factory = GsonConverterFactory.create(new GsonBuilder().create());
    private static Retrofit retrofit = null;
    private static volatile Service service = null;

    public static RetrofitHelper getInstance(Context context) {
        if (instance == null) {
            synchronized (RetrofitHelper.class) {
                instance = new RetrofitHelper(context);
            }
        }
        return instance;
    }


    private RetrofitHelper(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        resetApp();
    }
    //retrofit 初始化
    private void resetApp() {
        retrofit = new Retrofit.Builder()
                .baseUrl(ServiceApi.HOST)
                .client(new OkHttpClient())
                .addConverterFactory(factory)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }
    //获取 weatherServiceApi
    public Service getServer() {
        if (service == null){
            synchronized (Service.class){
                service = retrofit.create(Service.class);
            }
        }
        return service;

    }
}
